<template>
  <div class="container">
    <div class="app-container">
      <!-- 组织架构 -->
      <!-- default-expand-all  默认展开所有节点 -->
      <!-- data 组织架构数据 -->
      <!-- props 配置属性 -->
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <!-- 节点结构 -->
        <!-- v-slot="{ node, data }" 只能作用在template -->
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>

            <el-row>
              <span class="tree-manager" style="text-align: left;">{{ data.managerName }}</span>

              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="operateDept($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-row>

          </el-row>

        </template>
      </el-tree>
    </div>
    <!-- ref 可以获取dom 实例对象，也可以调用实例方法 -->
    <add-dept ref="addDept" :pid="pid" :show-dialog.sync="showDialog " @getDepartment="getDepartment" />
  </div>
</template>
<script>
import { getDepartmentAPI, deleteDepartmentAPI } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'

export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [], // 组织架构数据
      defaultProps: {
        label: 'name', // 要显示的字段名称
        children: 'children' // 子节点字段名称
      },
      showDialog: false,
      pid: null // 父级部门的id
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取组织架构
    async getDepartment() {
      const res = await getDepartmentAPI()
      console.log('获取组织架构  ----->  ', res)
      this.depts = transListToTreeData(res, 0)
    },
    operateDept(type, id) {
      switch (type) {
        case 'add':
          this.showDialog = true
          this.pid = id
          break
        case 'edit':
          this.showDialog = true
          this.pid = id // 当前部门id
          this.$nextTick(() => {
            this.$refs.addDept.getDepartmentDetail(id) // 调用子组件的方法
          })
          break
        case 'del':
          // 删除部门
          this.$confirm('您确认要删除该部门吗').then(async() => {
            await deleteDepartmentAPI(id)
            // 提示消息
            this.$message.success('删除部门成功')
            this.getDepartment()
          })
          break
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manager {
  width: 80px;
  display: inline-block;
  margin: 10px;
}
</style>
